﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using AutoLoL.Dialogs;

namespace AutoLoL.Controls
{
    /// <summary>
    /// Interaction logic for ChatItemControl.xaml
    /// </summary>
    public partial class ChatItemControl : UserControl
    {
        public ChatItemControl(int index, string title = "", List<ChatItemControl> children = null)
        {
            InitializeComponent();
            Index = Math.Max(1, Math.Min(6, index));
            Title = title;
            Children = children;

            IsEditable = false;
        }

        public int Index
        {
            get { return int.Parse(IndexTextBlock.Text); }
            set { IndexTextBlock.Text = value.ToString(); }
        }

        public string Title
        {
            get { return TitleTextBox.Text; }
            set { TitleTextBox.Text = value; }
        }

        public bool IsEditable
        {
            set
            {
                if (value)
                {
                    ButtonGrid.Visibility = Visibility.Visible;
                    TitleTextBlock.Visibility = Visibility.Collapsed;
                    TitleExpanderTextBlock.Visibility = Visibility.Collapsed;
                    TitleTextBox.Visibility = Visibility.Visible;
                    TitleExpanderTextBox.Visibility = Visibility.Visible;
                }
                else
                {
                    ButtonGrid.Visibility = Visibility.Collapsed;
                    TitleTextBlock.Visibility = Visibility.Visible;
                    TitleExpanderTextBlock.Visibility = Visibility.Visible;
                    TitleTextBox.Visibility = Visibility.Collapsed;
                    TitleExpanderTextBox.Visibility = Visibility.Collapsed;
                }

                if (Children != null)
                {
                    foreach (ChatItemControl child in Children)
                    {
                        child.IsEditable = value;
                    }
                }
            }
        }

        private List<ChatItemControl> _children;
        public List<ChatItemControl> Children
        {
            get
            {
                return _children;
            }
            set
            {
                _children = value;
                OnChildrenChanged();
            }
        }

        private void OnChildrenChanged()
        {
            if (_children != null)
            {
                foreach (ChatItemControl child in _children)
                {
                    ChildrenStackPanel.Children.Add(child);
                }

                CreateButton.Visibility = Visibility.Collapsed;
                DeleteButton.Visibility = Visibility.Visible;
                TitleGrid.Visibility = Visibility.Collapsed;
                ContentExpander.Visibility = Visibility.Visible;
            }
            else
            {
                ChildrenStackPanel.Children.Clear();

                CreateButton.Visibility = Visibility.Visible;
                DeleteButton.Visibility = Visibility.Collapsed;
                TitleGrid.Visibility = Visibility.Visible;
                ContentExpander.Visibility = Visibility.Collapsed;
            }
        }

        private void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            List<ChatItemControl> children = new List<ChatItemControl>();
            for (int i = 1; i <= 6; i++)
            {
                children.Add(new ChatItemControl(i)
                {
                    IsEditable = true
                });
            }

            Children = children;
            ContentExpander.IsExpanded = true;
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            ConfirmDialog dialog = new ConfirmDialog(Window.GetWindow(this), "Are you sure you wish to delete all child nodes?");
            dialog.ShowDialog();

            if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
            {
                Children = null;
            }
        }
    }
}
